package com.cscp.javastudy.juc.cas;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * CAS中的ABA的问题
 */
public class CasABA {

    public static void main(String[] args) {
        AtomicInteger atomicInteger = new AtomicInteger(2020);

        /*
         * public final boolean compareAndSet(int expect, int update)
         * expect: 期望的值
         * update: 要更新的新值
         * 如果是期望的值则更新，否则不更新
         */
        //捣乱的线程(将值从2020->2021->2020)
        System.out.println(atomicInteger.compareAndSet(2020, 2021));
        System.out.println(atomicInteger.get());//2021

        System.out.println(atomicInteger.compareAndSet(2021, 2020));
        System.out.println(atomicInteger.get());//2020

        //期望的线程
        System.out.println(atomicInteger.compareAndSet(2020, 6666));
        System.out.println(atomicInteger.get());//6666
    }
}
